﻿using System.Collections.Generic;
using Bingosoft.Data;
using Bingosoft.RestService.Attributes;

namespace Bingosoft.Modules.DataDictionary
{
    [Service("StorageService3")]
    public class StorageService3
    {
        private readonly Dao dao = Dao.Get();

        public void DeleteDataItem(string Id)
        {
            IDictionary<string, object> dictionary = new Dictionary<string, object> { { "Id", Id } };
            dao.ExecuteNonQuery("Storage3.delete", dictionary);
        }

        [ServiceMethod]
        public void DeleteDataDictionary(string dictionaryId)
        {
            IDictionary<string, object> dictionary = new Dictionary<string, object> { { "Id", dictionaryId } };
            dao.ExecuteNonQuery("dataDictionary.deleteItemsByDictionary", dictionary);
            dao.ExecuteNonQuery("dataDictionary.delete", dictionary);
        }

        public void SaveOrUpdateDictionaryItem(IDictionary<string, object> dictionary)
        {
            string id = dictionary["Id"] as string;
            if (string.IsNullOrEmpty(id))
            {
                dictionary.Add("Status", "enabled");
                dao.ExecuteNonQuery("dataDictionaryItem.insert", dictionary);
            }
            else
            {
                dao.ExecuteNonQuery("dataDictionaryItem.update", dictionary);
            }
        }

        public void Enable(string id)
        {
            IDictionary<string, object> dictionary = new Dictionary<string, object>
                                                         {
                                                             {"Status", "enabled"},
                                                             {"Id", id}
                                                         };
            dao.ExecuteNonQuery("dataDictionaryItem.updateStatus", dictionary);
        }

        public void Disable(string id)
        {
            IDictionary<string, object> dictionary = new Dictionary<string, object>
                                                         {
                                                             {"Status", "disabled"},
                                                             {"Id", id}
                                                         };
            dao.ExecuteNonQuery("dataDictionaryItem.updateStatus", dictionary);
        }

        public bool IsDictionaryCodeExist(string code, string id)
        {
            IDictionary<string, object> dictionary = new Dictionary<string, object> { { "Id", id }, { "Code", code } };
            int result =
                dao.QueryScalar<int>(
                    string.IsNullOrEmpty(id)
                        ? "dataDictionaryItem.checkUniqueCodeForInsert"
                        : "dataDictionaryItem.checkUniqueCodeForUpdate", dictionary);
            return result > 0;
        }
    }
}
